package com.xssq.mvc;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpContext {
	private static ThreadLocal<HttpContext> httpContextContainer = new ThreadLocal<>();
	
	private HttpServletRequest req;
	private HttpServletResponse resp;
	
	public static void init(HttpServletRequest req, HttpServletResponse resp) {
		HttpContext context = new HttpContext();
		context.req = req;
		context.resp = resp;
		httpContextContainer.set(context);
	}
	
	public static HttpServletRequest getRequest() {
		HttpContext context = httpContextContainer.get();
		if (null != context) {
			return context.req;
		}
		return null;
	}
	
	public static HttpServletResponse getResponse() {
		HttpContext context = httpContextContainer.get();
		if (null != context) {
			return context.resp;
		}
		return null;
	}
	
	public static ServletContext getServletContext() {
		HttpServletRequest request = getRequest();
		if (null != request) {
			return request.getServletContext();
		}
		return null;
	}
}
